package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.Address;
import com.abaaba.project2.bean.AddressExample;
import com.abaaba.project2.bean.bo.WxAddressBo;
import com.abaaba.project2.bean.vo.WxAddressDetailVo;
import com.abaaba.project2.bean.vo.WxAddressVo;
import com.abaaba.project2.mapper.AddressMapper;
import com.abaaba.project2.mapper.RegionMapper;
import com.abaaba.project2.service.wx.WxAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WxAddressServiceImpl implements WxAddressService {
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    RegionMapper regionMapper;

    @Override
    public List<WxAddressVo> getAddressList(Integer userId) {
        List<WxAddressVo> addressVoList = new ArrayList<>();
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andUserIdEqualTo(userId).andDeletedEqualTo(false);
        List<Address> addressList = addressMapper.selectByExample(addressExample);

        for (Address address : addressList) {
            String province = regionMapper.selectByPrimaryKey(address.getProvinceId()).getName();
            String city = regionMapper.selectByPrimaryKey(address.getCityId()).getName();
            String area = regionMapper.selectByPrimaryKey(address.getAreaId()).getName();

            addressVoList.add(new WxAddressVo(address.getIsDefault(),
                    province + city + area + " " + address.getAddress(),
                    address.getName(), address.getMobile(), address.getId()));
        }
        return addressVoList;
    }

    @Override
    public WxAddressDetailVo getAddressDetail(Integer id) {
        Address address = addressMapper.selectByPrimaryKey(id);
        String province = regionMapper.selectByPrimaryKey(address.getProvinceId()).getName();
        String city = regionMapper.selectByPrimaryKey(address.getCityId()).getName();
        String area = regionMapper.selectByPrimaryKey(address.getAreaId()).getName();
        WxAddressDetailVo wxAddressDetailVo = new WxAddressDetailVo(address.getIsDefault(), address.getId(),
                address.getProvinceId(), address.getCityId(), address.getAreaId(), province, city, area,
                address.getAddress(), address.getName(), address.getMobile());
        return wxAddressDetailVo;
    }

    @Override
    public Integer saveAddress(WxAddressBo address, Integer userId) {
        Address address1 = new Address(address.getId(), address.getName(), userId, address.getProvinceId(),
                address.getCityId(), address.getAreaId(), address.getAddress(), address.getMobile(), address.getIsDefault(),
                null, new Date(), null);

        // 若想设置默认地址，应先将原默认地址置于非默认
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        if (address1.getIsDefault()) {
            criteria.andIsDefaultEqualTo(true).andUserIdEqualTo(userId);
            List<Address> addressList = addressMapper.selectByExample(addressExample);
            if (addressList.size() == 1) {
                addressList.get(0).setIsDefault(false);
                addressMapper.updateByPrimaryKey(addressList.get(0));
            }
        }

        Integer affectedRows = 0;
        // 新建地址
        if (address.getId() == 0) {
            // TODO 扫描数据库
            //  1. 判断是否有相同已删除数据；
            // 2. 判断是否有相同未删除数据
            address1.setId(null);
            address1.setAddTime(new Date());
            affectedRows = addressMapper.insertSelective(address1);
        } else {
            // 修改地址
            affectedRows = addressMapper.updateByPrimaryKeySelective(address1);
        }

        return affectedRows;
    }

    @Override
    public Integer deleteAddress(Integer id) {
        Address address = addressMapper.selectByPrimaryKey(id);
        address.setIsDefault(false);
        address.setDeleted(true);
        return addressMapper.updateByPrimaryKeySelective(address);
    }
}
